package com.pjmike.netty.utils;

import io.netty.buffer.ByteBuf;

public class StringUtil {

    /**
     * @Author bj
     * @Description //获取bytebuf报文信息，十六进制显示
     * @Date 16:05 2021/1/11
     * @Param [byteBuf]
     * @return java.lang.String
     **/
    public static String getByteBufByteHexString(ByteBuf byteBuf){
        StringBuilder sb = new StringBuilder();
        byte[] bytes = new byte[byteBuf.readableBytes()];
        byteBuf.getBytes(0,bytes);
        for(byte b:bytes){
            sb.append(paddingByZero(Integer.toHexString(b),2));
            sb.append(' ');
        }
        return sb.toString();
    }
    /**
     * @Author bj
     * @Description //补零
     * @Date 16:18 2021/1/11
     * @Param [s, length]
     * @return java.lang.String
     **/
    public static String paddingByZero(String s,int length){
        return paddingByZero(s,length,false);
    }

    /**
     * @Author bj
     * @Description //补零
     * @Date 16:18 2021/1/11
     * @Param [s, length, isRight]
     * @return java.lang.String
     **/
    public static String paddingByZero(String s,int length,boolean isRight){
        return padding(s,length,'0',isRight);
    }
    /**
     * @Author bj
     * @Description //补位操作
     * @Date 16:18 2021/1/11
     * @Param [s, length, isRight]
     * @return java.lang.String
     **/
    public static String padding(String s,int length,char symbol,boolean isRight){
        StringBuilder ret = new StringBuilder(s);
        if(s.length()<length){
            for(int i=0;i<length-s.length();i++){
                if(isRight){
                    ret.append(symbol);
                }else{
                    ret.insert(0,symbol);
                }
            }
        }
        return ret.toString();
    }
}
